//
//  UIView+PopAnimation.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "UIView+PopAnimation.h"

//一个view从屏幕中心点弹出动画和消失动画

@implementation UIView (PopAnimation)


- (void)popViewWithanimated:(BOOL)animated
{
    // 保存当前弹出的视图
    CGFloat halfScreenWidth = [[UIScreen mainScreen] bounds].size.width * 0.5;
    CGFloat halfScreenHeight = [[UIScreen mainScreen] bounds].size.height * 0.5;
    // 屏幕中心
    CGPoint screenCenter = CGPointMake(halfScreenWidth, halfScreenHeight);
    self.center = screenCenter;
    
    if (animated) {
        // 第一步：将view宽高缩至无限小（点）
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity,CGFLOAT_MIN, CGFLOAT_MIN);
        [UIView animateWithDuration:0.3 animations:^{
            // 第二步： 以动画的形式将view慢慢放大至原始大小的1.2倍
            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
            // 第三步： 以动画的形式将view恢复至原始大小
            self.transform = CGAffineTransformIdentity;
            }];
        }];
    }
    
    
    /**
     *
     *第二种大致相同方法
     
     1.view初始化完后，设置其实transform值
     
     _containView.transform = CGAffineTransformMakeScale(0, 0);
     
     2.然后添加都控制期上后执行下面从中心点弹出动画
     
     - (void)handleShowAnimationEvent 
     {
     //UIViewAnimationOptionCurveLinear ：动画匀速执行，默认值。
     [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _containView.transform = CGAffineTransformMakeScale(1, 1);
     
     } completion:^(BOOL finished) {
        _containView.transform = CGAffineTransformIdentity;
     
     }];
     }
     
     
     *
     **/
    
}



- (void)closeAnimated:(BOOL)animated
{
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            // 第一步： 以动画的形式将view慢慢放大至原始大小的1.2倍
            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                // 第二步： 以动画的形式将view缩小至原来的1/1000分之1倍
                self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
            }completion:^(BOOL finished) {
                // 第三步： 移除
                [self removeFromSuperview];
            }];
        }];
    } else {
        [self removeFromSuperview];
    }
}


//左右震动效果
- (void)shake:(UIView *)view
{
    CGRect frame = view.frame;
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2);
    int index;
    for (index = 3; index >=0; --index) {
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 - frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 + frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
    }
    CGPathCloseSubpath(shakePath);
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = 0.5f;
    shakeAnimation.removedOnCompletion = YES;
    [view.layer addAnimation:shakeAnimation forKey:nil];
    CFRelease(shakePath);
}


@end
